package com.zhkj.shixi.tvfilemanager;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.zhkj.shixi.tvfilemanager.Interface.IDynamicDeviceInsertReceiver;
import com.zhkj.shixi.tvfilemanager.Interface.StartAction;
import com.zhkj.shixi.tvfilemanager.dialog.CommonDialog;
import com.zhkj.shixi.tvfilemanager.view.GrideViewPager;

import static com.zhkj.shixi.tvfilemanager.StaticClasses.deviceList;
import static com.zhkj.shixi.tvfilemanager.StaticClasses.fileManager;
import static com.zhkj.shixi.tvfilemanager.StaticClasses.staticReceiverAvailability;

/**
 * Created by linjing on 19-8-9.
 */

public class FileManagerAcitivity extends Activity implements StartAction,View.OnClickListener{
    private View mImageAudio;
    private View mImageVideo;
    private View mImageImg;
    private FileListActivity fileListActivity;
    private IDynamicDeviceInsertReceiver dynamicDeviceInsertReceiver = DynamicDeviceInsertReceiver.getInstance();
    private void init() {
        mImageAudio = findViewById(R.id.imageAudio);
        mImageVideo = findViewById(R.id.imageVideo);
        mImageImg = findViewById(R.id.imageImg);
        mImageAudio.setOnClickListener(this);
        mImageVideo.setOnClickListener(this);
        mImageImg.setOnClickListener(this);
        fileListActivity = new FileListActivity();
    }

    private void showNormalDialog(final Context context){
        final CommonDialog.Builder normalDialog =
                new CommonDialog.Builder(context);
        normalDialog.setTitle("退出？");
        normalDialog.setMessage("是否退出文件管理器？");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.w("dialog","press ok");
                        FileManagerAcitivity.super.onBackPressed();
                        dialog.dismiss();
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.w("dialog","press cancel");
                        dialog.dismiss();
                    }
                });
        // 显示
        CommonDialog dialog = normalDialog.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
        dialog.show();
    }
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.imageAudio:
                fileManager.setType(FileManager.MUSIC_TYPE);
                fileListActivity.startAction(this);
                break;
            case R.id.imageVideo:
                fileManager.setType(FileManager.VIDEO_TYPE);
                fileListActivity.startAction(this);
                break;
            case R.id.imageImg:
                fileManager.setType(FileManager.IMAGE_TYPE);

                fileListActivity.startAction(this);
                break;
        }
    }



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tv_file_manager_home);
        staticReceiverAvailability=false;//关闭静态广播接收
        dynamicDeviceInsertReceiver.startListener(this);//启动动态广播接收
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        fileManager.setCurrentURL(null);
        GrideViewPager.fileList = deviceList;
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        fileManager.setCurrentURL(null);
        GrideViewPager.fileList = deviceList;
    }

    @Override
    protected void onStart() {
        super.onStart();
        fileManager.setCurrentURL(null);
        GrideViewPager.fileList = deviceList;
    }

    @Override
    protected void onDestroy() {
        dynamicDeviceInsertReceiver.stopListener(this);
        staticReceiverAvailability=true;
        super.onDestroy();
    }

    @Override
    public void startAction(Context context) {

        Intent intent = new Intent(context, FileManagerAcitivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        showNormalDialog(this);
       // super.onBackPressed();
    }
}
